﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using StatisticsTableAdapters;

/// <summary>
/// Summary description for StatisticsBLL
/// </summary>
/// 
[System.ComponentModel.DataObject]
public class StatisticsBLL
{
    ILE_Global ileGlobal = new ILE_Global();

    private Stats_LoginTableAdapter _statsLoginAdapter = null;
    protected Stats_LoginTableAdapter StatsLoginAdapter
    {
        get
        {
            if (_statsLoginAdapter == null)
                _statsLoginAdapter = new Stats_LoginTableAdapter();

            return _statsLoginAdapter;
        }
    }

    private Stats_Quiz_QuestionsTableAdapter _statsQQAdapter = null;
    protected Stats_Quiz_QuestionsTableAdapter StatsQQAdapter
    {
        get
        {
            if (_statsQQAdapter == null)
                _statsQQAdapter = new Stats_Quiz_QuestionsTableAdapter();

            return _statsQQAdapter;
        }
    }

    private Stats_LearnObjsTableAdapter _statsLearnObjAdapter = null;
    protected Stats_LearnObjsTableAdapter StatsLearnObjAdapter
    {
        get
        {
            if (_statsLearnObjAdapter == null)
                _statsLearnObjAdapter = new Stats_LearnObjsTableAdapter();

            return _statsLearnObjAdapter;
        }
    }

    private Stats_LearnObjsUsersTableAdapter _statsLearnObjUsersAdapter = null;
    protected Stats_LearnObjsUsersTableAdapter StatsLearnObjUsersAdapter
    {
        get
        {
            if (_statsLearnObjUsersAdapter == null)
                _statsLearnObjUsersAdapter = new Stats_LearnObjsUsersTableAdapter();

            return _statsLearnObjUsersAdapter;
        }
    }

    #region Login Stats

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, true)]
    public Statistics.Stats_LoginDataTable GetStatsLogins()
    {
        return StatsLoginAdapter.GetStats_Logins();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LoginDataTable GetStatsLoginsByUsername(string Username)
    {
        return StatsLoginAdapter.GetStatsLoginByUsername(Username.ToLower());
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LoginDataTable GetStatsLoginsByDate(DateTime StartDate, DateTime EndDate)
    {
        return StatsLoginAdapter.GetStatsLoginByDate(StartDate, EndDate);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LoginDataTable GetStatsLoginsByAccountType(string accountType)
    {
        if (accountType == "All")
            return StatsLoginAdapter.GetStats_Logins();
        else
            return StatsLoginAdapter.GetStatsLoginByAccountType(accountType);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LoginRow GetStatsLoginByStatsLoginID(int statsLoginID)
    {
        try
        {
            return StatsLoginAdapter.GetStatsLoginByStatsLoginID(statsLoginID)[0];
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("StatisticsBLL:GetSTatsLoginByStatsLoginID:000 Failed to get StatsLogin row", ex, 5);
            return null;
        }
    }

    public bool IsLoginFailureGoingOn(string IpAddress, string Browser)
    {
        int result = 0;
        try
        {
            result = Convert.ToInt32(StatsLoginAdapter.CheckLoginFailure(IpAddress, DateTime.Now.AddMinutes(-3), Browser));
            if (result > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("StatisticsBLL.cs:IsLoginFailureGoingOn:000", ex, 4);
            return false;
        }
        
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LoginDataTable GetStatsLoginsForStatsPage(string NetworkID, string AccountType, string StartDateTime, string EndDateTime)
    {
        Statistics.Stats_LoginDataTable filteredLoginStats = new Statistics.Stats_LoginDataTable();

        if (StartDateTime != null && EndDateTime != null)
        {
            filteredLoginStats = GetStatsLoginsByDate(Convert.ToDateTime(StartDateTime), Convert.ToDateTime(EndDateTime).AddDays(1));
        }
        else if (StartDateTime != null && EndDateTime == null)
        {
            filteredLoginStats = GetStatsLoginsByDate(Convert.ToDateTime(StartDateTime), DateTime.Now.AddDays(2));
        }
        else if (StartDateTime == null && EndDateTime != null)
        {
            filteredLoginStats = GetStatsLoginsByDate(DateTime.Now.AddYears(-10), Convert.ToDateTime(EndDateTime).AddDays(1));
        }
        else
        {
            filteredLoginStats = GetStatsLoginsByAccountType(AccountType);
        }

        Statistics.Stats_LoginDataTable filteredLoginStats2 = new Statistics.Stats_LoginDataTable();

        foreach (Statistics.Stats_LoginRow row in filteredLoginStats)
        {
            filteredLoginStats2.ImportRow(row);
        }

        filteredLoginStats.Clear();

        foreach (Statistics.Stats_LoginRow row in filteredLoginStats2)
        {
            if (row.AccountType == AccountType || AccountType == "All")
            {
                filteredLoginStats.ImportRow(row);
            }
        }

        filteredLoginStats2.Clear();

        if (NetworkID != null)
        {
            foreach (Statistics.Stats_LoginRow row in filteredLoginStats)
            {
                if (row.Username.ToLower() == NetworkID.ToLower())
                {
                    filteredLoginStats2.ImportRow(row);
                }
            }
        }
        else
        {
            foreach (Statistics.Stats_LoginRow row in filteredLoginStats)
            {
                filteredLoginStats2.ImportRow(row);
            }
        }

        return filteredLoginStats2;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public void UpdateLogoutDateTime(DateTime logoutDateTime, int statsLoginID)
    {
        try
        {
            StatsLoginAdapter.UpdateStatsLogin(logoutDateTime, statsLoginID);
        }
        catch (Exception ex)
        {
            ileGlobal.RecordError("StatisticsBLL:UpdateLogoutDateTime:000 Failed to update logout time", ex, 3);
        }
    }


    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public int InsertStatsLogin(int userID, string ipAddress, string oS, string browser)
    {
        int result = Convert.ToInt32(StatsLoginAdapter.InsertStatsLogin(userID, DateTime.Now, ipAddress, oS, browser));
        return result;
    }
    #endregion



    #region Quiz Questions (QQ)

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_Quiz_QuestionsDataTable GetQuizQuestions()
    {
        return StatsQQAdapter.GetQuizQuestions();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public int CountByLearnObjID(int LearnObjID, bool IsAnsweredCorrectly)
    {
        return Convert.ToInt32(StatsQQAdapter.CountByLearnObjID(LearnObjID, IsAnsweredCorrectly));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public int CountByLearnObjIdAndQuestionId(int LearnObjID, int QuestionID, bool IsAnsweredCorrectly)
    {
        return Convert.ToInt32(StatsQQAdapter.CountByLearnObjIdAndQuestionId(LearnObjID, QuestionID, IsAnsweredCorrectly));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public int CountByLearnObjIdAndQuestionIdAndCourseIdAndSectionNumber(int LearnObjID, int QuestionID, int CourseID, int SectionNumber, bool IsAnsweredCorrectly)
    {
        return Convert.ToInt32(StatsQQAdapter.CountByLearnObjIdAndQuestionIdAndCourseIdAndSectionNumber(LearnObjID, QuestionID, CourseID, SectionNumber, IsAnsweredCorrectly));
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertQuizQuestionStats(int QuizSubmissionID, int LearnObjID, int QuestionID, bool IsAnsweredCorrectly, string WrongAnswer)
    {
        try
        {
            return StatsQQAdapter.Insert1(QuizSubmissionID, LearnObjID, QuestionID, IsAnsweredCorrectly, WrongAnswer, DateTime.Now) == 1;
        }
        catch
        {
            ileGlobal.RecordError("StatisticsBLL.cs:InsertQuizQuestionStats:001", "Could not insert Quiz Question Stats", 1);
            return false;
        }
    }

    #endregion

    #region Learning Objects

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Insert, false)]
    public bool InsertLearnObjStats(int learnObjID, int userID, int courseID, int sectionNumber, int standardID, DateTime viewDateTime)
    {
        bool result = StatsLearnObjAdapter.Insert(learnObjID, userID, courseID, sectionNumber, standardID, viewDateTime, DateTime.Now) == 1;
        return result;
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsDataTable GetLearnObjStats()
    {
        return StatsLearnObjAdapter.GetLearnObjsStats();
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsDataTable GetLearnObjStatsByCourseIdAndSectionNumber(int CourseID, int SectionNumber)
    {
        return StatsLearnObjAdapter.GetLearnObjsStatsByCourseIdAndSectionNumber(CourseID, SectionNumber);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsDataTable GetLearnObjStatsByCourseIdAndSectionNumberAndStandardId(int CourseID, int SectionNumber, int StandardID)
    {
        return StatsLearnObjAdapter.GetLearnObjsStatsByCourseIdandSectionNumberAndStandardId(CourseID, SectionNumber, StandardID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsDataTable GetLearnObjStatsByCourseIdAndSectionNumber(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return StatsLearnObjAdapter.GetLearnObjsStatsByCourseIdAndSectionNumberAndStandardIdAndLearnObjId(CourseID, SectionNumber, StandardID, LearnObjID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
        (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsDataTable GetLearnObjStatsByCourseIdAndSectionNumberAndStandardIdAndLearnObjID(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return StatsLearnObjAdapter.GetLearnObjsStatsByCourseIdAndSectionNumberAndStandardIdAndLearnObjId(CourseID, SectionNumber, StandardID, LearnObjID);
    }

    [System.ComponentModel.DataObjectMethodAttribute
       (System.ComponentModel.DataObjectMethodType.Select, false)]
    public Statistics.Stats_LearnObjsUsersDataTable GetLearnObjStatsUsers(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return StatsLearnObjUsersAdapter.GetLearnOBjStat(LearnObjID, CourseID, SectionNumber, StandardID);
    }

    public int CountUniqueNumberOfStudentsWentThorughLearnObj(int CourseID, int SectionNumber, int StandardID, int LearnObjID)
    {
        return Convert.ToInt32(StatsLearnObjAdapter.CountUniqueNumberOfStudentWentThroughLearnObj(CourseID, SectionNumber, StandardID, LearnObjID));
    }


    #endregion
}
